<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>pdf预览</title>
		<link rel="stylesheet" href="../css/mui.min.css">
		<link rel="stylesheet" href="../css/lzmui.css">
		<link rel="stylesheet" href="../css/pdf.css">
		<style type="text/css">
			html,body {padding: 0;margin: 0;width: 100%;height: 100%;}
		</style>
		
	</head>
	<body>
		<div id="App">
			<div id="pdf" style="width: 99vw; height:100vh;"></div>
			<!-- <iframe id="pdf" style="width: 99vw; height:100vh;overflow: hidden;" frameborder="0" :src="src" v-if="src"></iframe> -->
		</div>
		<!-- uni 的 SDK -->
		<script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script>
		<script type="text/javascript" src="../js/mui.min.js"></script>
		<script type="text/javascript" src="../js/vue.js"></script>
		<script type="text/javascript" src="../js/global.js"></script>
		<!-- 绘制水印电子签 -->
		<script src="https://cdn.bootcdn.net/ajax/libs/pdf-lib/1.5.1-rc1/pdf-lib.min.js"></script>
		<!-- 渲染pdf -->
		<script type="text/javascript" src="../js/pdfh5/pdf.js"></script>
		<script type="text/javascript" src="../js/pdfh5/pdf.worker.js"></script>
		<script type="text/javascript" src="../js/jquery.min.js"></script>
		<script type="text/javascript" src="../js/pdfh5/pdfh5.js"></script>
		
		<script type="text/javascript">
			// 为了解决uni-app 的web-view组件中加载mui开发的H5页面时，点击返回报错Uncaught ReferenceError: mui is not defined at __uniappview.html:1
			mui.init({
				keyEventBind: {
					backbutton: false //关闭back按键监听
				}
			});
			document.addEventListener('UniAppJSBridgeReady', function() {
				let that
				new Vue({
					el: '#App',
					data: {
						src:"",
						webView:{
							
						},
						seal_data:{}//请求到的后台数据
					},
					mounted() {
						that = this
						//获取传参数据
						that.getUrlParams()
					},
					methods: {
						//获取传参数据
						getUrlParams(){
							this.webView=JSON.parse(decodeURIComponent(GLOBAL_getUrlParam('data')))
							Global_params=JSON.parse(decodeURIComponent(GLOBAL_getUrlParam('data')))
							//console.log('接收到参数',this.webView);
							mui.showLoading('正在加载中..')
							if(this.webView.type=='申请单记录'){
								//请求详情
								this.getDataInfo()
							}else if(this.webView.type=='盖章记录'){
								//请求详情
								this.getDataInfoSeal()
							}else if(this.webView.type=='合同管理'){
								//请求详情
								this.getDataInfoContract()
							}
							
						},
						getDataInfoContract(){
							$lizhao.get(this.webView.api.GLOBAL_app_electronic_enterprise_contract_findEnterpriseContractInfo+'/'+this.webView.enterpriseContractId, '').then((res) => {
								if (res.code == 200) {
									that.seal_data=res.data
									//查看盖章记录
									that.seeSealRecordContract()
								}else{
									mui.toast(res.msg) 
									mui.hideLoading()
								}
							})
						},
						//请求详情
						getDataInfoSeal(){
							$lizhao.get(this.webView.api.GLOBAL_electronic_seal_info_findContractDetail+'/'+this.webView.sealInfoId+'?sealId='+this.webView.sealId, '').then((res) => {
								if (res.code == 200) {
									that.seal_data=res.data
									//查看盖章记录
									that.seeSealRecordSeal()
								}else{
									mui.toast(res.msg) 
									mui.hideLoading()
								}
							})
						},
						//请求详情
						getDataInfo(){
							$lizhao.post(this.webView.api.GLOBAL_application_getAuditorApplicationById, {applicationId:this.webView.applicationId},{'Content-Type': 'application/x-www-form-urlencoded'}).then((res) => {
								if (res.code == 200) {
									that.seal_data=res.data
									//查看盖章记录
									that.seeSealRecord()
								}else{
									mui.toast(res.msg) 
									mui.hideLoading()
								}
							})
						},
						//查看盖章记录
						seeSealRecordContract(){
							let url=this.seal_data.fileInfo.host+ this.seal_data.fileInfo.relativePath+'?Authorization='+this.webView.token
							let fileName=this.seal_data.fileInfo.originalName
							if(this.seal_data.liaisonSignContentList&&this.seal_data.liaisonSignContentList.length>0){
								let sealList= this.seal_data.liaisonSignContentList.map(item=>{
									if(item.content){
										try{
											item.signContent=JSON.parse(item.content)
										}catch(e){
											item.signContent=item.content
										}
									}
									return item
								})
								//console.log(sealList);
								//生成电子章，并下载
								this.modifyPdf(url,fileName,sealList,'预览')
							}else{
								this.modifyPdf(url,fileName,[],'预览')
							}
						},
						//查看盖章记录
						seeSealRecordSeal(){
							let url=this.seal_data.contractFile.host+ this.seal_data.contractFile.relativePath+'?Authorization='+this.webView.token
							let fileName=this.seal_data.contractFile.originalName
							if(this.seal_data.signedList&&this.seal_data.signedList.length>0){
								let sealList= this.seal_data.signedList.map(item=>{
									if(item.signContent){
										try{
											item.signContent=JSON.parse(item.signContent)
										}catch(e){
											item.signContent=item.signContent
										}
									}
									return item
								})
								//生成电子章，并下载
								this.modifyPdf(url,fileName,sealList,'预览')
							}else{
								this.modifyPdf(url,fileName,[],'预览')
							}
						},
						//查看盖章记录
						seeSealRecord(){
							let url=this.seal_data.contractFile.fileUrl+'?Authorization='+this.webView.token
							let fileName=this.seal_data.contractFile.name
							if(this.seal_data.signedList&&this.seal_data.signedList.length>0){
								let sealList= this.seal_data.signedList.map(item=>{
									if(item.signContent){
										try{
											item.signContent=JSON.parse(item.signContent)
										}catch(e){
											item.signContent=item.signContent
										}
									}
									return item
								})
								//生成电子章，并下载
								this.modifyPdf(url,fileName,sealList,'预览')
							}else{
								this.modifyPdf(url,fileName,[],'预览')
							}
						},
						async modifyPdf(url,fileName,sealList,type) {
							const existingPdfBytes = await fetch(url).then(res => res.arrayBuffer())
							const pdfDoc = await PDFLib.PDFDocument.load(existingPdfBytes)
							const pages = pdfDoc.getPages()
							if(sealList.length>0){
								sealList.forEach(async (item,index)=>{
									let pngImageBytes= $lizhao.base64.base64ToArrayBuffer(item.sealImageData)
									let pngImage=''
									try{
										pngImage = await pdfDoc.embedPng(pngImageBytes)
									}catch(e){
										pngImage = await pdfDoc.embedJpg(pngImageBytes)
									}
									//循环获取每一页，添加印章水印
									pages.forEach((page,pageNum)=>{
										if(!item.signContent){
											return
										}
										let obj =item.signContent[pageNum+1]
										const { width, height } = page.getSize()
										if(obj.isUse==true){
											obj.sealArr.forEach(sl=>{
												page.drawImage(pngImage,{
													x:(sl.left/obj.scale),
													y: height - (sl.top/obj.scale) -150,
													width: 150,
													height:150,
												})
											})
											// page.drawImage(pngImage,{
											// 	x:(obj.left/obj.scale),
											// 	y: height - (obj.top/obj.scale) -150,
											// 	width: 150,
											// 	height:150,
											// })
										}
									})
								})
							}
							setTimeout(async()=>{
								const pdfBytes = await pdfDoc.save()
								const blob = new Blob([pdfBytes], {type: 'application/pdf'});
								let pdfDataUri = URL.createObjectURL(blob);
								if(type=='预览'){
									var pdfh5 = new Pdfh5('#pdf', {
									  pdfurl: pdfDataUri
									});
									mui.hideLoading()
									// this.src=pdfDataUri
								}else if(type=='下载'){
									const a = document.createElement("a");
									a.href = pdfDataUri;
									a.download = fileName; // 这里填保存成的文件名
									a.click();
									URL.revokeObjectURL(a.href);
									a.remove();
								}
							},0)
						},
					}
				})
			});
		</script>
	</body>
</html>
